#include <iostream>
#include <cstdlib>
#include <sstream>
#include <time.h>

#include "Search.hxx"

struct Options
{
	std::string map_filename;
};

void show_help()
{
	std::cout << "Usage: grid-solver <map>" << std::endl;
	std::cout << "Examples:" << std::endl;

	std::cout << "             ./grid-solver map-38121-10" << std::endl;
	std::cout << "                 Solve  map-38121-10" << std::endl;
	std::cout << "                 initilized with Manhattan Distance from s to target cell" << std::endl;
}

bool process_command_line_args( int argc, char** argv, Options& opt  )
{
	if ( argc < 2 )
	{
		std::cerr << "Missing arguments!" << std::endl;
		show_help();
		return false;
	}

	opt.map_filename = argv[1];
	// Test that file exists and can be opened
	std::stringstream command_buffer;
	command_buffer << "test -f " << opt.map_filename;
	int res = system( command_buffer.str().c_str() );
	if ( res )
	{
		std::cout << "File " << opt.map_filename << " does not exist!" << std::endl;
		std::exit(1);
	}
	return true;
}

int main( int argc, char** argv )
{
	Options prog_opts;
	if ( !process_command_line_args( argc, argv, prog_opts ) )
		std::exit(1);

        Search * search = new Search(prog_opts.map_filename);
        
        double begTime = clock();
        search->a_star();
        double seconds = ((double) clock() - begTime) / CLOCKS_PER_SEC;
        
        search->printValues();
        search->writeValues();
        
        std::cout << seconds << std::endl;
        
        delete search;
        
	return 0;
}
